package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.vo.DuYanTeamVO;
import com.xbongbong.paas.service.CallCenterTeamService;
import com.xbongbong.paas.service.DuYanHttpService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CallCenterErrorCodeEnum;
import com.xbongbong.saas.domain.entity.CallCenterTeamEntity;
import com.xbongbong.saas.model.CallCenterTeamModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 呼叫中心团队信息实现层
 * @date 2019/2/21 10:41
 * @since v1.0
 */
@Service("callCenterTeamService")
public class CallCenterTeamServiceImpl implements CallCenterTeamService {

    @Resource
    private CallCenterTeamModel callCenterTeamModel;
    @Resource
    private DuYanHttpService duYanHttpService;

    /**
     * 根据销帮帮CRM的用户信息获取话务中心的团队账号
     *
     * @param corpid     公司id
     * @param depId      团队Id
     * @param moduleType 平台信息，参考CallCenterTypeEnum
     * @return CallCenterTeamEntity 话务中心团队信息 不存在则返回null
     */
    @Override
    public CallCenterTeamEntity getByXbbDepInfo(String corpid, Long depId, Integer moduleType) {
        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", corpid);
        params.put("depId", depId);
        params.put("moduleType", moduleType);
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("start", 0);
        params.put("pageNum", 1);
        List<CallCenterTeamEntity> callCenterTeamEntityList = callCenterTeamModel.findEntitys(params);
        return callCenterTeamEntityList.size() == 0 ? null : callCenterTeamEntityList.get(0);
    }

    /**
     * 获取远程的团队详情信息
     *
     * @param apiKey     第三方话务平台的公司key
     * @param teamId     团队id
     * @param moduleType 话务中心平台信息
     * @return CallCenterTeamEntity 封装的话务平台信息
     * @throws XbbException 封装的异常
     */
    @Override
    public CallCenterTeamEntity getOriginTeamDetail(String apiKey, String teamId, Integer moduleType) throws XbbException {
        CallCenterTeamEntity teamEntity = new CallCenterTeamEntity();
        if (StringUtil.isEmpty(teamId)) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301076);
        }
        DuYanTeamVO duYanTeamVO = duYanHttpService.getTeamDetail(apiKey, Long.parseLong(teamId));
        if (duYanTeamVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301077);
        }
        BeanUtils.copyProperties(duYanTeamVO, teamEntity);
        teamEntity.setAddTime(DateUtil.getInt());
        teamEntity.setUpdateTime(DateUtil.getInt());
        teamEntity.setDel(0);
        teamEntity.setTeamId(duYanTeamVO.getTeamId().toString());
        teamEntity.setIsPhoneLine(duYanTeamVO.getPhoneLine());
        return teamEntity;
    }

    /**
     * 同步远程的团队信息
     *
     * @param localTeam  本地团队信息
     * @param originTeam 远程团队信息
     * @throws XbbException 封装的异常
     */
    @Override
    public void syncOriginTeamDetail(CallCenterTeamEntity localTeam, CallCenterTeamEntity originTeam) throws XbbException {
        // 标记远程是否有更新（以远程为准）
        boolean isChanged = false;
        if (!Objects.equals(localTeam.getName(), originTeam.getName())) {
            localTeam.setName(originTeam.getName());
            isChanged = true;
        }
        if (!Objects.equals(localTeam.getPhoneVisibility(), originTeam.getPhoneVisibility())) {
            localTeam.setPhoneVisibility(originTeam.getPhoneVisibility());
            isChanged = true;
        }
        if (!Objects.equals(localTeam.getRemark(), originTeam.getRemark())) {
            localTeam.setRemark(originTeam.getRemark());
            isChanged = true;
        }
        if (isChanged) {
            callCenterTeamModel.update(localTeam);
        }
    }
}
